import allure
from selenium.webdriver.common.by import By

from page.home_page import HomePage
from utils.logger import logger


class WareManPage(HomePage):
    _ADD = (By.XPATH, "//*[text()='添加仓库（供应商）']")
    _INPUT_NAME = (By.XPATH, '//*[@class="inputWidth el-input"]//input')
    _TYPE = (By.XPATH, '//*[@placeholder="请选择"]')
    _奇门 = (By.XPATH, '//*[text()="奇门仓库"]')
    _CHASWARE = (By.XPATH, '//*[@class = "inputWidth el-input"]//input')
    _SUBMIT = (By.XPATH, "//*[text()='提交']")
    _CREATE_SUS = (By.XPATH, '//*[text()="创建成功"]')

    _REFRESH = (By.XPATH, "//*[text()='刷新负责人和群名称']")
    _REFRESH_TEXT = (By.XPATH, "//*[text()='负责人和群名称刷新成功']")
    _WARE_NAME = (By.XPATH, '//*[@placeholder = "请输入仓库(供应商)名称"]')
    _SEARCH = (By.XPATH, "//*[text()='查询']")
    _NUM = (By.CSS_SELECTOR, '.el-pagination__total')
    _DEL = (By.XPATH, "//*[text()='删除']")
    _OK = (By.CSS_SELECTOR, ".el-button.el-button--default.el-button--small.el-button--primary")
    _DEL_SUC = (By.XPATH, "//*[text()='删除成功']")
    _FIX = (By.XPATH, "//*[text()='修改']")

    def add_ware(self, name):
        logger.info("点击添加仓库")
        with allure.step("添加仓库"):
            self.find_and_click(self._ADD)
            self.find_and_send(name, self._INPUT_NAME)
            self.find_and_click(self._TYPE)
            self.set_time(1)
            self.finds(self._奇门)[1].click()
            self.finds(self._CHASWARE)[1].send_keys("1")
            self.finds(self._CHASWARE)[2].send_keys("1")
            self.finds(self._CHASWARE)[3].send_keys("1")
            self.find_and_click(self._SUBMIT)
            self.set_time(1)
        result = self.get_text(self._CREATE_SUS)
        return result

    def add_ware_no(self, name):
        logger.info("点击添加仓库")
        with allure.step("添加仓库"):
            self.find_and_click(self._ADD)
            self.find_and_send(name, self._INPUT_NAME)
            self.find_and_click(self._TYPE)
            self.set_time(1)
            self.finds(self._奇门)[1].click()
            self.find_and_click(self._SUBMIT)
            self.set_time(1)
        result = self.get_text(self._MESSAGE)
        self._driver.back()
        return result

    _MESSAGE = (By.CSS_SELECTOR, ".el-message__content")

    def add_ware_fail(self):
        logger.info("点击添加仓库")
        with allure.step("添加仓库"):
            self.find_and_click(self._ADD)
            self.set_time(1)
            self.find_and_click(self._TYPE)
            self.set_time(1)
            self.finds(self._奇门)[1].click()
            self.finds(self._CHASWARE)[1].send_keys("1")
            self.finds(self._CHASWARE)[2].send_keys("1")
            self.finds(self._CHASWARE)[3].send_keys("1")
            self.find_and_click(self._SUBMIT)
            self.set_time(1)
        result = self.get_text(self._MESSAGE)
        self._driver.back()
        return result

    def refresh(self):
        logger.info("刷新负责人和群名称")
        with allure.step("刷新负责人和群名称"):
            self.find_and_click(self._REFRESH)
            self.set_time(1)
            result = self.get_text(self._REFRESH_TEXT)
        return result

    _RESULT = (By.CSS_SELECTOR, ".el-table__row")

    @allure.step("输入筛选条件,查询")
    def search_suc(self, name):
        self.set_time(1)
        logger.info("输入筛选条件，查询")
        self.set_time(1)
        with allure.step("输入筛选条件，查询"):
            self.find_and_send(name, self._WARE_NAME)
            self.find_and_click(self._SEARCH)
            self.set_time(1)
        return len(self.finds(self._RESULT))

    _NO_DATA = (By.XPATH, '//*[text()="暂无数据"]')

    @allure.step('搜索不存在的仓库')
    def search_fail(self, name):
        with allure.step("输入仓库名称"):
            self.find_and_send(name, self._WARE_NAME)
        with allure.step("点击查询"):
            self.find_and_click(self._SEARCH)
        return self.get_text(self._NO_DATA)

    _TRAJECTORY = (By.XPATH, "//*[text()='操作轨迹']")
    _CREATE_WARE = (By.XPATH, '//*[text()="创建仓库"]')

    @allure.step('查看操作轨迹')
    def look_trajectory(self, name):
        with allure.step("输入仓库名称"):
            self.find_and_send(name, self._WARE_NAME)
        with allure.step("点击查询"):
            self.find_and_click(self._SEARCH)

        with allure.step("点击操作轨迹"):
            # self.find_and_click(self._TRAJECTORY)
            element = self.find(self._TRAJECTORY)
            # arguments[0]代表所传值element的第一个参数
            # click()代表js中的点击动作
            self._driver.execute_script("arguments[0].click();", element)

        result = self.get_text(self._CREATE_WARE)
        self._driver.refresh()
        return result

    @allure.step("删除仓库")
    def del_ware(self, name):
        logger.info("删除仓库")
        with allure.step("输入仓库名称"):
            self.find_and_send(name, self._WARE_NAME)
        with allure.step("点击查询"):
            self.find_and_click(self._SEARCH)
        with allure.step("删除仓库"):
            # self.find_and_click(self._DEL)
            # arguments[0]代表所传值element的第一个参数
            # click()代表js中的点击动作
            self._driver.execute_script("arguments[0].click();", self.find(self._DEL))
            self.find_and_click(self._OK)
            self.set_time(1.5)
            self.wait_for_visible(self._DEL_SUC)
        result = self.get_text(self._DEL_SUC)
        return result
